<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/Frog_32px_1177822_easyicon.net.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/Frog_16px_1177822_easyicon.net.ico">
  <link rel="mask-icon" href="/images/Frog_32px_1177822_easyicon.net.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"hxy1997.xyz","root":"/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"输入关键字","hits_empty":"没有找到与「${query}」相关搜索","hits_stats":"${hits} 条相关记录，共耗时 ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="React Quizzes考察了对于React的理解，是基于16.8版本以上的React hooks了。">
<meta property="og:type" content="article">
<meta property="og:title" content="bigfrontend的React quizzes">
<meta property="og:url" content="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/index.html">
<meta property="og:site_name" content="hxy的博客">
<meta property="og:description" content="React Quizzes考察了对于React的理解，是基于16.8版本以上的React hooks了。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-06-17T13:07:33.000Z">
<meta property="article:modified_time" content="2022-08-19T01:07:13.804Z">
<meta property="article:author" content="hxy">
<meta property="article:tag" content="React">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="bigfrontend">
<meta property="article:tag" content="题库">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>bigfrontend的React quizzes | hxy的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="hxy的博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">hxy的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Mia san Mia!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/huxingyi1997" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://hxy1997.xyz/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/Robben.gif">
      <meta itemprop="name" content="hxy">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="hxy的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          bigfrontend的React quizzes
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-06-17 21:07:33" itemprop="dateCreated datePublished" datetime="2022-06-17T21:07:33+08:00">2022-06-17</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-08-19 09:07:13" itemprop="dateModified" datetime="2022-08-19T09:07:13+08:00">2022-08-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/web%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">web前端</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">热度：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2022/06/17/bigfrontend%E7%9A%84React%20quizzes/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>React Quizzes考察了对于React的理解，是基于16.8版本以上的React hooks了。</p>
<span id="more"></span>

<h1 id="React-Quizzes"><a href="#React-Quizzes" class="headerlink" title="React Quizzes"></a>React Quizzes</h1><p>该部分综合考察对React的熟悉程度</p>
<h2 id="1-React-re-render-1"><a href="#1-React-re-render-1" class="headerlink" title="1. React re-render 1"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/React-re-render-1">1. React re-render 1</a></h2><h3 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;B&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return &lt;C&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A state&#x3D;&#123;state&#125;&#x2F;&gt;</span><br><span class="line">      &lt;D&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案"><a href="#答案" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析"><a href="#详细解析" class="headerlink" title="详细解析"></a>详细解析</h3><p>there are three reasons that will cause re-rendering</p>
<ul>
<li>update in state</li>
<li>update in prop</li>
<li>re-rendering of parent component</li>
</ul>
<p>One thing to note, while parent component called the re-rendering function, the sebsequent will rerender whether their props change or not</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; useState, useEffect, memo &#125; from &quot;react&quot;;</span><br><span class="line">import ReactDOM from &quot;react-dom&quot;;</span><br><span class="line"></span><br><span class="line">&#x2F;* </span><br><span class="line">Whenever component re-renders, does it mean that React re-renders the real DOM each time? </span><br><span class="line">No, React only updates the part of the UI that changed. </span><br><span class="line">A render is scheduled by React each time the state of a component is modified. </span><br><span class="line">For example, updating state via the setState hook will not happen immediately but React will execute it at the best possible moment.</span><br><span class="line"></span><br><span class="line">But calling the render function has some side-effects even if the real DOM is not re-rendered:</span><br><span class="line">    - the code inside the function is executed each time, which can be time-consuming depending on its content</span><br><span class="line">    - the diffing algorithm is executed for each component to be able to determine if the UI needs to be updated</span><br><span class="line">*&#x2F;</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&quot;A&quot;);</span><br><span class="line">  return &lt;B &#x2F;&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&quot;B&quot;);</span><br><span class="line">  return &lt;C &#x2F;&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&quot;C&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&quot;D&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;* </span><br><span class="line">Each state change in the parent component triggers a re-rendering of the child </span><br><span class="line">components even if they did not receive any props.</span><br><span class="line"></span><br><span class="line">We can prevent children components from this needless rendering but wraping them</span><br><span class="line">inside &#96;React.memo&#96;. They then child won&#39;t render on state change of parent. They </span><br><span class="line">will re-render only when props passed to them change. Even after wrapping in &#96;memo&#96;</span><br><span class="line">they will always render on change of their internal state.</span><br><span class="line"></span><br><span class="line">However when child component re-renders then its parent component is not re-rendered.</span><br><span class="line">*&#x2F;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0);</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState((state) &#x3D;&gt; state + 1);</span><br><span class="line">  &#125;, []);</span><br><span class="line">  console.log(&quot;App&quot;);</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A state&#x3D;&#123;state&#125; &#x2F;&gt;</span><br><span class="line">      &lt;D &#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App &#x2F;&gt;, document.getElementById(&quot;root&quot;));</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line"> *&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="2-React-re-render-2-memo"><a href="#2-React-re-render-2-memo" class="headerlink" title="2. React re-render 2 - memo()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/React-re-render-2">2. React re-render 2 - memo()</a></h2><h3 id="题目-1"><a href="#题目-1" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect, memo &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;B&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const B &#x3D; memo(() &#x3D;&gt; &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return &lt;C&#x2F;&gt;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A state&#x3D;&#123;state&#125;&#x2F;&gt;</span><br><span class="line">      &lt;D&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-1"><a href="#答案-1" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-1"><a href="#详细解析-1" class="headerlink" title="详细解析"></a>详细解析</h3><p>only rerender while the memorized value is different from the previous value</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; useState, useEffect, memo &#125; from &quot;react&quot;;</span><br><span class="line">import ReactDOM from &quot;react-dom&quot;;</span><br><span class="line"></span><br><span class="line">&#x2F;* </span><br><span class="line">Whenever component re-renders, does it mean that React re-renders the real DOM each time? </span><br><span class="line">No, React only updates the part of the UI that changed. </span><br><span class="line">A render is scheduled by React each time the state of a component is modified. </span><br><span class="line">For example, updating state via the setState hook will not happen immediately but React will execute it at the best possible moment.</span><br><span class="line"></span><br><span class="line">But calling the render function has some side-effects even if the real DOM is not re-rendered:</span><br><span class="line">    - the code inside the function is executed each time, which can be time-consuming depending on its content</span><br><span class="line">    - the diffing algorithm is executed for each component to be able to determine if the UI needs to be updated</span><br><span class="line">*&#x2F;</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&quot;A&quot;);</span><br><span class="line">  return &lt;B &#x2F;&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&quot;B&quot;);</span><br><span class="line">  return &lt;C &#x2F;&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&quot;C&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&quot;D&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#x2F;* </span><br><span class="line">Each state change in the parent component triggers a re-rendering of the child </span><br><span class="line">components even if they did not receive any props.</span><br><span class="line"></span><br><span class="line">We can prevent children components from this needless rendering but wraping them</span><br><span class="line">inside &#96;React.memo&#96;. They then child won&#39;t render on state change of parent. They </span><br><span class="line">will re-render only when props passed to them change. Even after wrapping in &#96;memo&#96;</span><br><span class="line">they will always render on change of their internal state.</span><br><span class="line"></span><br><span class="line">However when child component re-renders then its parent component is not re-rendered.</span><br><span class="line">*&#x2F;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0);</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState((state) &#x3D;&gt; state + 1);</span><br><span class="line">  &#125;, []);</span><br><span class="line">  console.log(&quot;App&quot;);</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A state&#x3D;&#123;state&#125; &#x2F;&gt;</span><br><span class="line">      &lt;D &#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App &#x2F;&gt;, document.getElementById(&quot;root&quot;));</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line">&quot;App&quot;   &#x2F;&#x2F; first render before useEffect() worked</span><br><span class="line">&quot;A&quot;     &#x2F;&#x2F; first render</span><br><span class="line">&quot;B&quot;     &#x2F;&#x2F; first render</span><br><span class="line">&quot;C&quot;     &#x2F;&#x2F; first render</span><br><span class="line">&quot;D&quot;     &#x2F;&#x2F; first render</span><br><span class="line">&quot;App&quot;   &#x2F;&#x2F; second render after useEffect() worked</span><br><span class="line">&quot;A&quot;     &#x2F;&#x2F; second render -&gt; no B because of memo() -&gt; no C because no B</span><br><span class="line">&quot;D&quot;     &#x2F;&#x2F; second render</span><br><span class="line"> *&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="3-React-re-render-3"><a href="#3-React-re-render-3" class="headerlink" title="3. React re-render 3"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/React-re-render-3">3. React re-render 3</a></h2><h3 id="题目-2"><a href="#题目-2" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return &lt;C&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A&gt;&lt;B&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">      &lt;D&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-2"><a href="#答案-2" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-2"><a href="#详细解析-2" class="headerlink" title="详细解析"></a>详细解析</h3><p>When rerendering function is called in parent component, the sebsequent component will re-render whether they are changed or not</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return &lt;C&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A&gt;&lt;B&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">      &lt;D&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line"> *&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="4-React-re-render-4"><a href="#4-React-re-render-4" class="headerlink" title="4. React re-render 4"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/React-re-render-3">4. React re-render 4</a></h2><h3 id="题目-3"><a href="#题目-3" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return &lt;C&#x2F;&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A&gt;&lt;B&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">      &lt;D&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  )</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-3"><a href="#答案-3" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-3"><a href="#详细解析-3" class="headerlink" title="详细解析"></a>详细解析</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; useState, useEffect &#125; from &quot;react&quot;;</span><br><span class="line">import ReactDOM from &quot;react-dom&quot;;</span><br><span class="line"></span><br><span class="line">function A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&quot;A&quot;);</span><br><span class="line">  const [state, setState] &#x3D; useState(0);</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState((state) &#x3D;&gt; state + 1);</span><br><span class="line">  &#125;, []);</span><br><span class="line">  &#x2F;* Here even on state change of &#96;A&#96; parent component, child components will not re-render. When the parent state</span><br><span class="line">   changes, parent component re-renders. But it still has the same children prop it got last time, </span><br><span class="line">   so React doesn’t visit that subtree. And as a result, child component doesn’t re-render.</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line">  Thus there are two ways to </span><br><span class="line">  prevent child components from re-rendering.</span><br><span class="line">    - wrapping them in &#96;memeo&#96;</span><br><span class="line">    - passing them as &#96;children&#96; prop</span><br><span class="line"> *&#x2F;</span><br><span class="line">  return children;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  console.log(&quot;B&quot;);</span><br><span class="line">  return &lt;C &#x2F;&gt;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&quot;C&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&quot;D&quot;);</span><br><span class="line">  return null;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&quot;App&quot;);</span><br><span class="line">  return (</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">      &lt;A&gt;</span><br><span class="line">        &lt;B &#x2F;&gt;</span><br><span class="line">      &lt;&#x2F;A&gt;</span><br><span class="line">      &lt;D &#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">  );</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App &#x2F;&gt;, document.getElementById(&quot;root&quot;));</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="6-React-re-render-5-context"><a href="#6-React-re-render-5-context" class="headerlink" title="6. React re-render 5 - context"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/React-re-render-5">6. React re-render 5 - context</a></h2><h3 id="题目-4"><a href="#题目-4" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, memo, createContext, useEffect, useContext&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const MyContext &#x3D; createContext(0);</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  const count &#x3D; useContext(MyContext)</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(() &#x3D;&gt; &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;B&#x2F;&gt;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;MyContext.Provider value&#x3D;&#123;state&#125;&gt;</span><br><span class="line">    &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;C&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;MyContext.Provider&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-4"><a href="#答案-4" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-4"><a href="#详细解析-4" class="headerlink" title="详细解析"></a>详细解析</h3><p>context is like “global”</p>
<ul>
<li>All of the descendants of a Provider will rerender whenever the Provider’s value is updated</li>
<li>Even though therir parents component doesn’t update</li>
<li>Consumers is not subject to the shouleComponentUpdate</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, memo, createContext, useEffect, useContext&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const MyContext &#x3D; createContext(0);</span><br><span class="line"></span><br><span class="line">function B() &#123;</span><br><span class="line">  const count &#x3D; useContext(MyContext)</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F; When the second time render comes, A would not be re-render again for the memo &#x2F;&#x2F; method, but the computed value contains component B, which called useContext </span><br><span class="line">&#x2F;&#x2F; with a modified value of MyContext. so B would re-render, answer below</span><br><span class="line">&#x2F;&#x2F; &quot;App&quot; &quot;A&quot; &quot;B&quot; &quot;C&quot; &quot;App&quot; &quot;B&quot; &quot;C&quot;</span><br><span class="line">const A &#x3D; memo(() &#x3D;&gt; &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;B&#x2F;&gt;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line">function App() &#123;</span><br><span class="line">	&#x2F;&#x2F; state is change. This same state is passed in</span><br><span class="line">    &#x2F;&#x2F; context. So all those compoonets that use this context and</span><br><span class="line">    &#x2F;&#x2F; their children will re-render</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;MyContext.Provider value&#x3D;&#123;state&#125;&gt;</span><br><span class="line">    &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;C&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;MyContext.Provider&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>



<h2 id="7-Suspense-1"><a href="#7-Suspense-1" class="headerlink" title="7. Suspense 1"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/Suspense-1">7. Suspense 1</a></h2><h3 id="题目-5"><a href="#题目-5" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; Suspense &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const resource &#x3D; (() &#x3D;&gt; &#123;</span><br><span class="line">  let data &#x3D; null</span><br><span class="line">  let status &#x3D; &#39;pending&#39;</span><br><span class="line">  let fetcher &#x3D; null</span><br><span class="line">  return &#123;</span><br><span class="line">    get() &#123;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;ready&#39;) &#123;</span><br><span class="line">        return data</span><br><span class="line">      &#125;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;pending&#39;) &#123;</span><br><span class="line">        fetcher &#x3D; new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">          setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            data &#x3D; 1</span><br><span class="line">            status &#x3D; &#39;ready&#39;</span><br><span class="line">            resolve()</span><br><span class="line">          &#125;, 100)</span><br><span class="line">        &#125;)</span><br><span class="line">        status &#x3D; &#39;fetching&#39;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      throw fetcher</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A1&#39;)</span><br><span class="line">  const data &#x3D; resource.get()</span><br><span class="line">  console.log(&#39;A2&#39;)</span><br><span class="line">  return &lt;p&gt;&#123;data&#125;&lt;&#x2F;p&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Fallback() &#123;</span><br><span class="line">  console.log(&#39;fallback&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;Suspense fallback&#x3D;&#123;&lt;Fallback&#x2F;&gt;&#125;&gt;</span><br><span class="line">      &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;Suspense&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-5"><a href="#答案-5" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A1&quot;</span></span><br><span class="line"><span class="string">&quot;fallback&quot;</span></span><br><span class="line"><span class="string">&quot;A1&quot;</span></span><br><span class="line"><span class="string">&quot;A2&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-5"><a href="#详细解析-5" class="headerlink" title="详细解析"></a>详细解析</h3><ul>
<li>“suspend” rendering in order to wait for some async resource</li>
<li>re-render again when the resources finnally loaded</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; Suspense &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const resource &#x3D; (() &#x3D;&gt; &#123;</span><br><span class="line">  let data &#x3D; null</span><br><span class="line">  let status &#x3D; &#39;pending&#39;</span><br><span class="line">  let fetcher &#x3D; null</span><br><span class="line">  return &#123;</span><br><span class="line">    get() &#123;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;ready&#39;) &#123;</span><br><span class="line">        return data</span><br><span class="line">      &#125;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;pending&#39;) &#123;</span><br><span class="line">        fetcher &#x3D; new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">          setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            data &#x3D; 1</span><br><span class="line">            status &#x3D; &#39;ready&#39;</span><br><span class="line">            resolve()</span><br><span class="line">          &#125;, 100)</span><br><span class="line">        &#125;)</span><br><span class="line">        status &#x3D; &#39;fetching&#39;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      throw fetcher</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A1&#39;)</span><br><span class="line">  const data &#x3D; resource.get()</span><br><span class="line">  console.log(&#39;A2&#39;)</span><br><span class="line">  return &lt;p&gt;&#123;data&#125;&lt;&#x2F;p&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Fallback() &#123;</span><br><span class="line">  console.log(&#39;fallback&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;Suspense fallback&#x3D;&#123;&lt;Fallback&#x2F;&gt;&#125;&gt;</span><br><span class="line">      &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;Suspense&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A1&quot;</span><br><span class="line">&quot;fallback&quot;</span><br><span class="line">&quot;A1&quot;</span><br><span class="line">&quot;A2&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="8-Suspense-2"><a href="#8-Suspense-2" class="headerlink" title="8. Suspense 2"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/Suspense-2">8. Suspense 2</a></h2><h3 id="题目-6"><a href="#题目-6" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; Suspense &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const resource &#x3D; (() &#x3D;&gt; &#123;</span><br><span class="line">  let data &#x3D; null</span><br><span class="line">  let status &#x3D; &#39;pending&#39;</span><br><span class="line">  let fetcher &#x3D; null</span><br><span class="line">  return &#123;</span><br><span class="line">    get() &#123;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;ready&#39;) &#123;</span><br><span class="line">        return data</span><br><span class="line">      &#125;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;pending&#39;) &#123;</span><br><span class="line">        fetcher &#x3D; new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">          setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            data &#x3D; 1</span><br><span class="line">            status &#x3D; &#39;ready&#39;</span><br><span class="line">            resolve()</span><br><span class="line">          &#125;, 100)</span><br><span class="line">        &#125;)</span><br><span class="line">        status &#x3D; &#39;fetching&#39;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      throw fetcher</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A1&#39;)</span><br><span class="line">  const data &#x3D; resource.get()</span><br><span class="line">  console.log(&#39;A2&#39;)</span><br><span class="line">  return &lt;p&gt;&#123;data&#125;&lt;&#x2F;p&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Fallback() &#123;</span><br><span class="line">  console.log(&#39;fallback&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;Suspense fallback&#x3D;&#123;&lt;Fallback&#x2F;&gt;&#125;&gt;</span><br><span class="line">      &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;Suspense&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-6"><a href="#答案-6" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A1&quot;</span></span><br><span class="line"><span class="string">&quot;fallback&quot;</span></span><br><span class="line"><span class="string">&quot;A1&quot;</span></span><br><span class="line"><span class="string">&quot;A2&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-6"><a href="#详细解析-6" class="headerlink" title="详细解析"></a>详细解析</h3><p>Key:</p>
<p>Suspense component will be rendered when a child ran into async render (but other children will be rendered first) and all children in Suspense will be re-rendered after async resolve</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; Suspense &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const resource &#x3D; (() &#x3D;&gt; &#123;</span><br><span class="line">  let data &#x3D; null</span><br><span class="line">  let status &#x3D; &#39;pending&#39;</span><br><span class="line">  let fetcher &#x3D; null</span><br><span class="line">  return &#123;</span><br><span class="line">    get() &#123;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;ready&#39;) &#123;</span><br><span class="line">        return data</span><br><span class="line">      &#125;</span><br><span class="line">      if (status &#x3D;&#x3D;&#x3D; &#39;pending&#39;) &#123;</span><br><span class="line">        fetcher &#x3D; new Promise((resolve, reject) &#x3D;&gt; &#123;</span><br><span class="line">          setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">            data &#x3D; 1</span><br><span class="line">            status &#x3D; &#39;ready&#39;</span><br><span class="line">            resolve()</span><br><span class="line">          &#125;, 100)</span><br><span class="line">        &#125;)</span><br><span class="line">        status &#x3D; &#39;fetching&#39;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      throw fetcher</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">function A() &#123;</span><br><span class="line">  console.log(&#39;A1&#39;)</span><br><span class="line">  const data &#x3D; resource.get()</span><br><span class="line">  console.log(&#39;A2&#39;)</span><br><span class="line">  return &lt;p&gt;&#123;data&#125;&lt;&#x2F;p&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function Fallback() &#123;</span><br><span class="line">  console.log(&#39;fallback&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;Suspense fallback&#x3D;&#123;&lt;Fallback&#x2F;&gt;&#125;&gt;</span><br><span class="line">      &lt;A&#x2F;&gt;</span><br><span class="line">    &lt;&#x2F;Suspense&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A1&quot;</span><br><span class="line">&quot;fallback&quot;</span><br><span class="line">&quot;A1&quot;</span><br><span class="line">&quot;A2&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="9-React-re-render-6-Context"><a href="#9-React-re-render-6-Context" class="headerlink" title="9. React re-render 6 - Context"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/react-rerender-6-context">9. React re-render 6 - Context</a></h2><h3 id="题目-7"><a href="#题目-7" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, createContext, useEffect, useContext&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const MyContext &#x3D; createContext(0);</span><br><span class="line"></span><br><span class="line">function B(&#123;children&#125;) &#123;</span><br><span class="line">  const count &#x3D; useContext(MyContext)</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; (&#123;children&#125;) &#x3D;&gt; &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  return &lt;MyContext.Provider value&#x3D;&#123;state&#125;&gt;</span><br><span class="line">    &#123;children&#125;</span><br><span class="line">  &lt;&#x2F;MyContext.Provider&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;A&gt;&lt;B&gt;&lt;C&#x2F;&gt;&lt;&#x2F;B&gt;&lt;D&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-7"><a href="#答案-7" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;C&quot;</span></span><br><span class="line"><span class="string">&quot;D&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-7"><a href="#详细解析-7" class="headerlink" title="详细解析"></a>详细解析</h3><ol>
<li>Render as usual in order of children</li>
<li>When re render from state occurs, we rerender A and all consumers of the context. Theres no need to rerender the other children. So only B is rerendered on the context change</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, createContext, useEffect, useContext&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">const MyContext &#x3D; createContext(0);</span><br><span class="line"></span><br><span class="line">function B(&#123;children&#125;) &#123;</span><br><span class="line">  const count &#x3D; useContext(MyContext)</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; (&#123;children&#125;) &#x3D;&gt; &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line">  return &lt;MyContext.Provider value&#x3D;&#123;state&#125;&gt;</span><br><span class="line">    &#123;children&#125;</span><br><span class="line">  &lt;&#x2F;MyContext.Provider&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function C() &#123;</span><br><span class="line">  console.log(&#39;C&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">function D() &#123;</span><br><span class="line">  console.log(&#39;D&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  return &lt;A&gt;&lt;B&gt;&lt;C&#x2F;&gt;&lt;&#x2F;B&gt;&lt;D&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;C&quot;</span><br><span class="line">&quot;D&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="10-useLayoutEffect"><a href="#10-useLayoutEffect" class="headerlink" title="10. useLayoutEffect()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/useLayoutEffect">10. useLayoutEffect()</a></h2><h3 id="题目-8"><a href="#题目-8" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState, useEffect, useLayoutEffect&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useEffect 1&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useEffect 1 cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useEffect 2&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useEffect 2 cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  useLayoutEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useLayoutEffect&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useLayoutEffect cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br></pre></td></tr></table></figure>

<h3 id="答案-8"><a href="#答案-8" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;useLayoutEffect&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 1&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 2&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;useLayoutEffect cleanup&quot;</span></span><br><span class="line"><span class="string">&quot;useLayoutEffect&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 1 cleanup&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 2 cleanup&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 1&quot;</span></span><br><span class="line"><span class="string">&quot;useEffect 2&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-8"><a href="#详细解析-8" class="headerlink" title="详细解析"></a>详细解析</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; useState, useEffect, useLayoutEffect&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useEffect 1&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useEffect 1 cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useEffect 2&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useEffect 2 cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  useLayoutEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;useLayoutEffect&#39;)</span><br><span class="line">    return () &#x3D;&gt; &#123;</span><br><span class="line">      console.log(&#39;useLayoutEffect cleanup&#39;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;, [state])</span><br><span class="line"></span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;)</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot; &#x2F;&#x2F; Initial rendering cycle doesn&#39;t run any clean up.</span><br><span class="line">&quot;useLayoutEffect&quot;</span><br><span class="line">&quot;useEffect 1&quot;</span><br><span class="line">&quot;useEffect 2&quot;</span><br><span class="line">&quot;App&quot; &#x2F;&#x2F; Re-render</span><br><span class="line">&quot;useLayoutEffect cleanup&quot; &#x2F;&#x2F; useLayoutEffect is first to be cleaned up and immediately executed.</span><br><span class="line">&quot;useLayoutEffect&quot;</span><br><span class="line">&quot;useEffect 1 cleanup&quot; &#x2F;&#x2F; Regular useEffects are grouped, cleaned up and then executed for the second rendering cycle.</span><br><span class="line">&quot;useEffect 2 cleanup&quot;</span><br><span class="line">&quot;useEffect 1&quot;</span><br><span class="line">&quot;useEffect 2&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="11-callback-props"><a href="#11-callback-props" class="headerlink" title="11. callback props"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/useLayoutEffect">11. callback props</a></h2><h3 id="题目-9"><a href="#题目-9" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; memo, useState&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen, fireEvent &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line"></span><br><span class="line">function _A(&#123; onClick &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;button onClick&#x3D;&#123;onClick&#125; data-testid&#x3D;&quot;button&quot;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &#123;state&#125;</span><br><span class="line">    &lt;A onClick&#x3D;&#123;() &#x3D;&gt; &#123;setState(state &#x3D;&gt; state + 1)&#125;&#125;&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">;(async function() &#123;</span><br><span class="line">  const button &#x3D; await screen.findByTestId(&#39;button&#39;)</span><br><span class="line">  fireEvent.click(button)</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>

<h3 id="答案-9"><a href="#答案-9" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-9"><a href="#详细解析-9" class="headerlink" title="详细解析"></a>详细解析</h3><p>a new function gets created on every render and React.memo thinks that the prop has changed</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; memo, useState&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen, fireEvent &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line"></span><br><span class="line">function _A(&#123; onClick &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;button onClick&#x3D;&#123;onClick&#125; data-testid&#x3D;&quot;button&quot;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &#123;state&#125;</span><br><span class="line">    &lt;A onClick&#x3D;&#123;() &#x3D;&gt; &#123;setState(state &#x3D;&gt; state + 1)&#125;&#125;&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">;(async function() &#123;</span><br><span class="line">  const button &#x3D; await screen.findByTestId(&#39;button&#39;)</span><br><span class="line">  fireEvent.click(button)</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="11-callback-props-1"><a href="#11-callback-props-1" class="headerlink" title="11. callback props"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/callback-props">11. callback props</a></h2><h3 id="题目-10"><a href="#题目-10" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; memo, useState&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen, fireEvent &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line"></span><br><span class="line">function _A(&#123; onClick &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;button onClick&#x3D;&#123;onClick&#125; data-testid&#x3D;&quot;button&quot;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &#123;state&#125;</span><br><span class="line">    &lt;A onClick&#x3D;&#123;() &#x3D;&gt; &#123;setState(state &#x3D;&gt; state + 1)&#125;&#125;&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">;(async function() &#123;</span><br><span class="line">  const button &#x3D; await screen.findByTestId(&#39;button&#39;)</span><br><span class="line">  fireEvent.click(button)</span><br><span class="line">&#125;)()</span><br></pre></td></tr></table></figure>

<h3 id="答案-10"><a href="#答案-10" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;App&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-10"><a href="#详细解析-10" class="headerlink" title="详细解析"></a>详细解析</h3><p>a new function gets created on every render and React.memo thinks that the prop has changed</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; memo, useState&#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen, fireEvent &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line"></span><br><span class="line">function _A(&#123; onClick &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return &lt;button onClick&#x3D;&#123;onClick&#125; data-testid&#x3D;&quot;button&quot;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  console.log(&#39;App&#39;)</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &#123;state&#125;</span><br><span class="line">    &lt;A onClick&#x3D;&#123;() &#x3D;&gt; &#123;setState(state &#x3D;&gt; state + 1)&#125;&#125;&#x2F;&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">;(async function() &#123;</span><br><span class="line">  const button &#x3D; await screen.findByTestId(&#39;button&#39;)</span><br><span class="line">  fireEvent.click(button)</span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;App&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="12-useEffect"><a href="#12-useEffect" class="headerlink" title="12. useEffect"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/useEffect">12. useEffect</a></h2><h3 id="题目-11"><a href="#题目-11" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useEffect, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  console.log(state)</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(state)</span><br><span class="line">    setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      console.log(state)</span><br><span class="line">    &#125;, 100)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br></pre></td></tr></table></figure>

<h3 id="答案-11"><a href="#答案-11" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">0</span><br><span class="line">0</span><br><span class="line">1</span><br><span class="line">0</span><br></pre></td></tr></table></figure>

<h3 id="详细解析-11"><a href="#详细解析-11" class="headerlink" title="详细解析"></a>详细解析</h3><p>1st render</p>
<p>console.log(0) 【collect】</p>
<p>after 1st render</p>
<p>callback: setState function</p>
<p>console.log(state), now is still 0 【collect】</p>
<p>callback: setTimeOut(wait for 100 and then fires callback)</p>
<p>start working on the callback queue:</p>
<p>setState -&gt; state=1</p>
<p>setTimeOut(wait for 100 and then fires callback)</p>
<p>2nd render</p>
<p>console.log(1) 【collect】</p>
<p>the 100ms passed, fires the setTimeOut function, here because of closure, console.log(0) 【collect】</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useEffect, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  console.log(state)</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    console.log(state)</span><br><span class="line">    setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      console.log(state)</span><br><span class="line">    &#125;, 100)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">0</span><br><span class="line">0</span><br><span class="line">1</span><br><span class="line">0</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="13-useRef"><a href="#13-useRef" class="headerlink" title="13. useRef"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/useRef">13. useRef</a></h2><h3 id="题目-12"><a href="#题目-12" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useRef, useEffect, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const ref &#x3D; useRef(null)</span><br><span class="line">  const [state, setState] &#x3D; useState(1)</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(2)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  console.log(ref.current?.textContent)</span><br><span class="line"></span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;div ref&#x3D;&#123;state &#x3D;&#x3D;&#x3D; 1 ? ref : null&#125;&gt;1&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div ref&#x3D;&#123;state &#x3D;&#x3D;&#x3D; 2 ? ref : null&#125;&gt;2&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br></pre></td></tr></table></figure>

<h3 id="答案-12"><a href="#答案-12" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">undefined</span><br><span class="line"><span class="string">&quot;1&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-12"><a href="#详细解析-12" class="headerlink" title="详细解析"></a>详细解析</h3><p>//undefined - because on initial render ref is //set to null and <code>null?.textContent</code> evaluates //to undefined</p>
<p>//“1” because during initial render current of //ref is set to the first div with textContent //value 1, this value is preserved/cached //when //useEffect/setState triggers a re-render</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useRef, useEffect, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const ref &#x3D; useRef(null)</span><br><span class="line">  const [state, setState] &#x3D; useState(1)</span><br><span class="line"></span><br><span class="line">  useEffect(() &#x3D;&gt; &#123;</span><br><span class="line">    setState(2)</span><br><span class="line">  &#125;, [])</span><br><span class="line"></span><br><span class="line">  console.log(ref.current?.textContent)</span><br><span class="line"></span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;div ref&#x3D;&#123;state &#x3D;&#x3D;&#x3D; 1 ? ref : null&#125;&gt;1&lt;&#x2F;div&gt;</span><br><span class="line">    &lt;div ref&#x3D;&#123;state &#x3D;&#x3D;&#x3D; 2 ? ref : null&#125;&gt;2&lt;&#x2F;div&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const root &#x3D; ReactDOM.createRoot(document.getElementById(&#39;root&#39;));</span><br><span class="line">root.render(&lt;App&#x2F;&gt;);</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">undefined</span><br><span class="line">&quot;1&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="14-async-event-handler"><a href="#14-async-event-handler" class="headerlink" title="14. async event handler"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/async-event-handler">14. async event handler</a></h2><h3 id="题目-13"><a href="#题目-13" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const increment &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      setState(state + 1)</span><br><span class="line">    &#125;, 0)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;increment&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button twice</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br></pre></td></tr></table></figure>

<h3 id="答案-13"><a href="#答案-13" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">0</span><br><span class="line">1</span><br><span class="line">1</span><br></pre></td></tr></table></figure>

<h3 id="详细解析-13"><a href="#详细解析-13" class="headerlink" title="详细解析"></a>详细解析</h3><p>Key:</p>
<p>setTimeout(() =&gt; {}, 0) put setState call in a marcro task with the current state bind into closure</p>
<p>that’s why in the second and third render, states are both 1.</p>
<p>Answer:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev </span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const increment &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    setTimeout(() &#x3D;&gt; &#123;</span><br><span class="line">      setState(state + 1)</span><br><span class="line">    &#125;, 0)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;increment&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F; click the button twice</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">0</span><br><span class="line">1</span><br><span class="line">1</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="15-memo-2"><a href="#15-memo-2" class="headerlink" title="15. memo 2"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/memo-2">15. memo 2</a></h2><h3 id="题目-14"><a href="#题目-14" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">import React, &#123; memo, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function _B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const B &#x3D; memo(_B)</span><br><span class="line"></span><br><span class="line">function _A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [count, setCount] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;</span><br><span class="line">      () &#x3D;&gt; setCount(count &#x3D;&gt; count + 1)</span><br><span class="line">    &#125;&gt;</span><br><span class="line">      click me</span><br><span class="line">    &lt;&#x2F;button&gt;</span><br><span class="line">    &lt;A&gt;&lt;B&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line"></span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br></pre></td></tr></table></figure>

<h3 id="答案-14"><a href="#答案-14" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;A&quot;</span></span><br><span class="line"><span class="string">&quot;B&quot;</span></span><br><span class="line"><span class="string">&quot;A&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-14"><a href="#详细解析-14" class="headerlink" title="详细解析"></a>详细解析</h3><p>first render, both component will be rendered<br>“A”<br>“B”<br>second render, since children in A doesn’t have any props change, B will return memorized render result directly, not do re-render, hence only A get rendered<br>“A”</p>
<p><a target="_blank" rel="noopener" href="https://gist.github.com/slikts/e224b924612d53c1b61f359cfb962c06">https://gist.github.com/slikts/e224b924612d53c1b61f359cfb962c06</a></p>
<p><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/47567429/this-props-children-not-re-rendered-on-parent-state-change">https://stackoverflow.com/questions/47567429/this-props-children-not-re-rendered-on-parent-state-change</a></p>
<p>Here’s what I understood from those articles, still not 100% sure, but hope can give you some ideas</p>
<p>Essentially, when App render</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&lt;A&gt;</span><br><span class="line">    &lt;B /&gt;</span><br><span class="line">&lt;/A&gt;</span><br></pre></td></tr></table></figure>

<p>jsx will create a new React element that pass to A</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  type: A,</span><br><span class="line">  props: &#123;</span><br><span class="line">    children: &#123; memorized B &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Note, A’s props.children is a new object, so even you memorized A, it still re-rendered</p>
<p>when react render down to B, since B’s props didn’t change, so it returned memoized result (even it’s a new React Element to A, it’s rendering result is the same one)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">import React, &#123; memo, useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function _B() &#123;</span><br><span class="line">  console.log(&#39;B&#39;)</span><br><span class="line">  return null</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const B &#x3D; memo(_B)</span><br><span class="line"></span><br><span class="line">function _A(&#123; children &#125;) &#123;</span><br><span class="line">  console.log(&#39;A&#39;)</span><br><span class="line">  return children</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">const A &#x3D; memo(_A)</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [count, setCount] &#x3D; useState(0)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;</span><br><span class="line">      () &#x3D;&gt; setCount(count &#x3D;&gt; count + 1)</span><br><span class="line">    &#125;&gt;</span><br><span class="line">      click me</span><br><span class="line">    &lt;&#x2F;button&gt;</span><br><span class="line">    &lt;A&gt;&lt;B&#x2F;&gt;&lt;&#x2F;A&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line"></span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;A&quot;</span><br><span class="line">&quot;B&quot;</span><br><span class="line">&quot;A&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="16-event-callback"><a href="#16-event-callback" class="headerlink" title="16. event callback"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/memo-2">16. event callback</a></h2><h3 id="题目-15"><a href="#题目-15" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev</span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const onClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;handler&#39;)</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">    console.log(&#39;handler &#39; + state)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#39;render &#39; + state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;onClick&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br></pre></td></tr></table></figure>

<h3 id="答案-15"><a href="#答案-15" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;render 0&quot;</span></span><br><span class="line"><span class="string">&quot;handler&quot;</span></span><br><span class="line"><span class="string">&quot;handler 0&quot;</span></span><br><span class="line"><span class="string">&quot;render 1&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-15"><a href="#详细解析-15" class="headerlink" title="详细解析"></a>详细解析</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev</span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const onClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;handler&#39;)</span><br><span class="line">    setState(state &#x3D;&gt; state + 1)</span><br><span class="line">    console.log(&#39;handler &#39; + state)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#39;render &#39; + state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;onClick&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;render 0&quot;</span><br><span class="line">&quot;handler&quot;</span><br><span class="line">&quot;handler 0&quot;</span><br><span class="line">&quot;render 1&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>



<h2 id="17-flushSync"><a href="#17-flushSync" class="headerlink" title="17. flushSync()"></a><a target="_blank" rel="noopener" href="https://bigfrontend.dev/react-quiz/flushsync">17. flushSync()</a></h2><h3 id="题目-16"><a href="#题目-16" class="headerlink" title="题目"></a>题目</h3><p>What does the code snippet to the right output by <code>console.log</code>?</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev</span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM, &#123; flushSync &#125; from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const onClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;handler&#39;)</span><br><span class="line">    flushSync(() &#x3D;&gt; &#123;</span><br><span class="line">      setState(state &#x3D;&gt; state + 1)</span><br><span class="line">    &#125;)</span><br><span class="line">    console.log(&#39;handler &#39; + state)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#39;render &#39; + state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;onClick&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br></pre></td></tr></table></figure>

<h3 id="答案-16"><a href="#答案-16" class="headerlink" title="答案"></a>答案</h3><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;render 0&quot;</span></span><br><span class="line"><span class="string">&quot;handler&quot;</span></span><br><span class="line"><span class="string">&quot;render 1&quot;</span></span><br><span class="line"><span class="string">&quot;handler 0&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="详细解析-16"><a href="#详细解析-16" class="headerlink" title="详细解析"></a>详细解析</h3><p>FlushSync flushes the entire tree and actually forces complete re-rendering for updates that happen inside of a call, so you should use it very sparingly. This way it doesn’t break the guarantee of internal consistency between props, state, and refs.</p>
<p><code>&quot;render 0&quot;</code>: logged the first time the DOM is rendered</p>
<p>Then the click event happens</p>
<p><code>&quot;handler&quot;</code>: logged when the handler for the click fires</p>
<p><code>&quot;render 1&quot;</code>: logged first because <code>flushSync</code> causes the DOM to <em>synchronously</em> get re-rendered with the new state</p>
<p><code>&quot;handler 0&quot;</code>: logged after <code>flushSync</code>, but using the state value that was available when this function first initialized</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; This is a React Quiz from BFE.dev</span><br><span class="line"></span><br><span class="line">import React, &#123; useState &#125; from &#39;react&#39;</span><br><span class="line">import ReactDOM, &#123; flushSync &#125; from &#39;react-dom&#39;</span><br><span class="line">import &#123; screen &#125; from &#39;@testing-library&#x2F;dom&#39;</span><br><span class="line">import userEvent from &#39;@testing-library&#x2F;user-event&#39;</span><br><span class="line"></span><br><span class="line">function App() &#123;</span><br><span class="line">  const [state, setState] &#x3D; useState(0)</span><br><span class="line">  const onClick &#x3D; () &#x3D;&gt; &#123;</span><br><span class="line">    console.log(&#39;handler&#39;)</span><br><span class="line">    flushSync(() &#x3D;&gt; &#123;</span><br><span class="line">      setState(state &#x3D;&gt; state + 1)</span><br><span class="line">    &#125;)</span><br><span class="line">    console.log(&#39;handler &#39; + state)</span><br><span class="line">  &#125;</span><br><span class="line">  console.log(&#39;render &#39; + state)</span><br><span class="line">  return &lt;div&gt;</span><br><span class="line">    &lt;button onClick&#x3D;&#123;onClick&#125;&gt;click me&lt;&#x2F;button&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">ReactDOM.render(&lt;App&#x2F;&gt;, document.getElementById(&#39;root&#39;))</span><br><span class="line">&#x2F;&#x2F; click the button</span><br><span class="line">userEvent.click(screen.getByText(&#39;click me&#39;))</span><br><span class="line"></span><br><span class="line">&#x2F;* Ans:</span><br><span class="line"></span><br><span class="line">&quot;render 0&quot;</span><br><span class="line">&quot;handler&quot;</span><br><span class="line">&quot;render 1&quot;</span><br><span class="line">&quot;handler 0&quot;</span><br><span class="line"></span><br><span class="line">*&#x2F;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    
      
<div>
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
</div>
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/React/" rel="tag"><i class="fa fa-tag"></i> React</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 前端</a>
              <a href="/tags/bigfrontend/" rel="tag"><i class="fa fa-tag"></i> bigfrontend</a>
              <a href="/tags/%E9%A2%98%E5%BA%93/" rel="tag"><i class="fa fa-tag"></i> 题库</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/06/12/%E8%87%AA%E5%B7%B1%E7%9A%84%E7%BC%BA%E7%82%B9%E6%80%9D%E8%80%83/" rel="prev" title="自己的缺点思考">
      <i class="fa fa-chevron-left"></i> 自己的缺点思考
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/06/17/bigfrontend%E7%9A%84React%20problems/" rel="next" title="bigfrontend的React problems">
      bigfrontend的React problems <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#React-Quizzes"><span class="nav-text">React Quizzes</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-React-re-render-1"><span class="nav-text">1. React re-render 1</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-React-re-render-2-memo"><span class="nav-text">2. React re-render 2 - memo()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-1"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-1"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-1"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-React-re-render-3"><span class="nav-text">3. React re-render 3</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-2"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-2"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-2"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-React-re-render-4"><span class="nav-text">4. React re-render 4</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-3"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-3"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-3"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-React-re-render-5-context"><span class="nav-text">6. React re-render 5 - context</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-4"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-4"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-4"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-Suspense-1"><span class="nav-text">7. Suspense 1</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-5"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-5"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-5"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-Suspense-2"><span class="nav-text">8. Suspense 2</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-6"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-6"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-6"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-React-re-render-6-Context"><span class="nav-text">9. React re-render 6 - Context</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-7"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-7"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-7"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-useLayoutEffect"><span class="nav-text">10. useLayoutEffect()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-8"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-8"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-8"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-callback-props"><span class="nav-text">11. callback props</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-9"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-9"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-9"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-callback-props-1"><span class="nav-text">11. callback props</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-10"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-10"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-10"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#12-useEffect"><span class="nav-text">12. useEffect</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-11"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-11"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-11"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#13-useRef"><span class="nav-text">13. useRef</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-12"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-12"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-12"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#14-async-event-handler"><span class="nav-text">14. async event handler</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-13"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-13"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-13"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#15-memo-2"><span class="nav-text">15. memo 2</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-14"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-14"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-14"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#16-event-callback"><span class="nav-text">16. event callback</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-15"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-15"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-15"><span class="nav-text">详细解析</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#17-flushSync"><span class="nav-text">17. flushSync()</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A2%98%E7%9B%AE-16"><span class="nav-text">题目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%AD%94%E6%A1%88-16"><span class="nav-text">答案</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AF%A6%E7%BB%86%E8%A7%A3%E6%9E%90-16"><span class="nav-text">详细解析</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="hxy"
      src="/images/Robben.gif">
  <p class="site-author-name" itemprop="name">hxy</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">80</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">120</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/huxingyi1997" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;huxingyi1997" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:huxingyi1997@zju.edu.cn" title="E-Mail → mailto:huxingyi1997@zju.edu.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-frog"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">hxy</span>
</div>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共1039.2k字</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'pQsO3ySbU4VtWN2j1FLA74Ha-gzGzoHsz',
      appKey     : 'QYacMDY2VY7Wazprg1X6FiUv',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

  
  <!-- 动态背景特效 -->
  <!-- 樱花特效 -->
    <script async src="/js/src/sakura.js"></script>
    <script async src="/js/src/fairyDustCursor.js"></script>
</body>
</html>
